﻿using Sodu.Util;
using Sodu.Util.HttpHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkID=390556 上有介绍

namespace Sodu.Pages
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Page_Login : Page
    {
        public event HandleResult Handle;


        string Html = string.Empty;

        public Page_Login()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。
        /// 此参数通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ///重写返回键事件
            Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
        }

        void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            e.Handled = true;
            if (this.progress.Visibility == Visibility.Visible)
            {
                try
                {
                    HttpHelper.webRequest.Abort();
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    this.progress.Visibility = Visibility.Collapsed;
                }
            }
            else
            {
                if (Frame != null && Frame.CanGoBack)
                {
                    Frame.GoBack();
                }
            }
        }




        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancle_Click(object sender, RoutedEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                Frame.GoBack();
            }
        }
        /// <summary>
        /// 登录提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSumbit_Click(object sender, RoutedEventArgs e)
        {

            if (string.IsNullOrWhiteSpace(this.txtName.Text))
            {
                MessageBox.Show("账号不能为空。");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.txtPassword.Password))
            {
                MessageBox.Show("密码不能为空。");
                return;
            }

            this.progress.Visibility = Visibility.Visible;

            string uri = SoduConstants.SoduContantsValue.LoginPostPage;
            string postdata = "username=" + this.txtName.Text + "&password=" + this.txtPassword.Password + "&B1=%B5%C7%C2%BC&postcheck=true";
            HttpHelper.HttpPostRequest(uri, GetDataCompleted, postdata);
        }


        public void GetDataCompleted(string html)
        {
            this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                   {
                       if (html.Contains("登录成功"))
                       {
                           SoduConstants.SoduContantsValue.UserName = this.txtName.Text;
                           SoduConstants.SoduContantsValue.IfLogin = true;

                           Frame.Navigate(typeof(Page_Loaded));
                       }
                       else if (html.Contains("用户名或密码错误，请重新登录!"))
                       {
                           MessageBox.Show("账号或密码错误，请重新输入。");
                           this.progress.Visibility = Visibility.Collapsed;
                       }
                   }
                    );
        }


        /// <summary>
        /// 点击回车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                if (FocusManager.GetFocusedElement() == this.txtName)
                {
                    this.txtPassword.Focus(FocusState.Keyboard);
                }
                else if (FocusManager.GetFocusedElement() == this.txtSearchPara)
                {
                    btnSumbit_Click(null, null);
                }
                else
                {
                    return;
                }
            }
        }


        /// <summary>
        /// 点击图片logo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Image_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            Frame.Navigate(typeof(MainPage));
        }

        /// <summary>
        /// 点击搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtSearchPara.Text))
            {
                MessageBox.Show("请输入搜索关键字");
                return;
            }
            // Frame.Navigate(typeof(Page_SearchResult), this.txtSearchPara.Text.Trim());
        }
    }
}
